package com.llyb.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.llyb.lease.common.exception.LeaseException;
import com.llyb.lease.common.result.ResultCodeEnum;
import com.llyb.lease.model.entity.*;
import com.llyb.lease.model.enums.ItemType;
import com.llyb.lease.model.enums.LeaseStatus;
import com.llyb.lease.web.admin.mapper.RoomInfoMapper;
import com.llyb.lease.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.llyb.lease.web.admin.vo.graph.GraphVo;
import com.llyb.lease.web.admin.vo.room.RoomDetailVo;
import com.llyb.lease.web.admin.vo.room.RoomItemVo;
import com.llyb.lease.web.admin.vo.room.RoomQueryVo;
import com.llyb.lease.web.admin.vo.room.RoomSubmitVo;
import io.minio.messages.Item;
import org.apache.ibatis.reflection.ArrayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.management.Attribute;
import java.sql.Array;
import java.util.ArrayList;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private RoomAttrValueService roomAttrValueService;

    @Autowired
    private RoomLabelService roomLabelService;

    @Autowired
    private RoomFacilityService roomFacilityService;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private LeaseAgreementService leaseAgreementService;
    @Override
    public void customFindAll(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        roomInfoMapper.customSelectAll(page,queryVo);
    }

    @Override
    public void customSaveOrUpdate(RoomSubmitVo roomSubmitVo) {

        boolean isUpdated = roomSubmitVo.getId() != null;

        // 更新
        saveOrUpdate(roomSubmitVo);
        Long id = roomSubmitVo.getId();
        // 修改中间表
        if (isUpdated) {
            updateMiddle(id);
        }

        Long roomId = roomSubmitVo.getId();

        // 添加六个表
        if (!CollectionUtils.isEmpty(roomSubmitVo.getAttrValueIds())) {
            ArrayList<RoomAttrValue> roomAttrValueArrayList = new ArrayList<>();
            for (Long attrValueId : roomSubmitVo.getAttrValueIds()) {
                RoomAttrValue build = RoomAttrValue.builder().attrValueId(attrValueId).roomId(roomId).build();
                roomAttrValueArrayList.add(build);
            }
            roomAttrValueService.saveBatch(roomAttrValueArrayList);
        }
        if (!CollectionUtils.isEmpty(roomSubmitVo.getFacilityInfoIds())) {
            ArrayList<RoomFacility> roomFacilityArrayList = new ArrayList<>();
            for (Long facilityInfoId : roomSubmitVo.getFacilityInfoIds()) {
                RoomFacility build = RoomFacility.builder().facilityId(facilityInfoId).roomId(roomId).build();
                roomFacilityArrayList.add(build);
            }
            roomFacilityService.saveBatch(roomFacilityArrayList);
        }
        if (!CollectionUtils.isEmpty(roomSubmitVo.getLabelInfoIds())) {
            ArrayList<RoomLabel> labelArrayList = new ArrayList<>();
            for (Long labelInfoId : roomSubmitVo.getLabelInfoIds()) {
                RoomLabel build = RoomLabel.builder().labelId(labelInfoId).roomId(roomId).build();
                labelArrayList.add(build);
            }
            roomLabelService.saveBatch(labelArrayList);
        }
        if (!CollectionUtils.isEmpty(roomSubmitVo.getPaymentTypeIds())) {
            ArrayList<RoomPaymentType> paymentArrayList = new ArrayList<>();
            for (Long paymentTypeId : roomSubmitVo.getPaymentTypeIds()) {
                RoomPaymentType build = RoomPaymentType.builder().roomId(roomId).paymentTypeId(paymentTypeId).build();
                paymentArrayList.add(build);
            }
            roomPaymentTypeService.saveBatch(paymentArrayList);
        }
        if (!CollectionUtils.isEmpty(roomSubmitVo.getLeaseTermIds())) {
            ArrayList<RoomLeaseTerm> leaseArrayList = new ArrayList<>();
            for (Long leaseTermId : roomSubmitVo.getLeaseTermIds()) {
                RoomLeaseTerm build = RoomLeaseTerm.builder().leaseTermId(leaseTermId).roomId(roomId).build();
                leaseArrayList.add(build);
            }
            roomLeaseTermService.saveBatch(leaseArrayList);
        }
        if (!CollectionUtils.isEmpty(roomSubmitVo.getGraphVoList())) {
            ArrayList<GraphInfo> graphInfoArrayList = new ArrayList<>();
            for (GraphVo graphVo : roomSubmitVo.getGraphVoList()) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(id);
                graphInfoArrayList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoArrayList);
        }
    }

    @Override
    public RoomDetailVo customGetById(Long id) {
        return roomInfoMapper.customSelectById(id);
    }

    @Override
    public void customRemoveById(Long id) {
        // 查询是否存在租约、存在租约的话，不允许删除
        LambdaQueryWrapper<LeaseAgreement> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LeaseAgreement::getRoomId,id);
        lambdaQueryWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED,LeaseStatus.RENEWING,LeaseStatus.WITHDRAWING);
        long count = leaseAgreementService.count(lambdaQueryWrapper);

        if (count>0) {
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

        // 删除中间表
        updateMiddle(id);

        removeById(id);
    }

    private void updateMiddle(Long id) {
        LambdaQueryWrapper<RoomAttrValue> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(lambdaQueryWrapper);

        LambdaQueryWrapper<RoomLabel> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(lambdaQueryWrapper1);

        LambdaQueryWrapper<RoomFacility> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(lambdaQueryWrapper2);

        LambdaQueryWrapper<RoomPaymentType> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper3.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(lambdaQueryWrapper3);

        LambdaQueryWrapper<RoomLeaseTerm> lambdaQueryWrapper4 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper4.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(lambdaQueryWrapper4);

        LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper5 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper5.eq(GraphInfo::getItemId, id);
        lambdaQueryWrapper5.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphInfoService.remove(lambdaQueryWrapper5);
    }
}




